package cn.dzhang.rcp.hyperbola.common;

import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.Platform;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.ui.IViewSite;
import org.eclipse.ui.model.IWorkbenchAdapter;
import org.eclipse.ui.model.IWorkbenchAdapter2;
import org.eclipse.ui.model.IWorkbenchAdapter3;

import cn.dzhang.rcp.hyperbola.Activator;
import cn.dzhang.rcp.hyperbola.adapters.IWorkbenchAdapter4;
import cn.dzhang.rcp.hyperbola.model.Alarm;
import cn.dzhang.rcp.hyperbola.model.AlarmLevel;

public abstract class UIUtils {
	public static void setMessage(IViewSite site, String message) {
		setMessage(null, message);
	}

	public static void setMessage(IViewSite site, Image image, String message) {
		site.getActionBars().getStatusLineManager().setMessage(image, message);
	}

	public static void setErrorMessage(IViewSite site, String message) {
		setErrorMessage(null, message);
	}

	public static void setErrorMessage(IViewSite site, Image image,
			String message) {
		site.getActionBars().getStatusLineManager()
				.setErrorMessage(image, message);
	}

	public static void setDefaultBackgroundColor(Composite composite) {
		setBackgroundColor(composite,
				UIConstants.Colors.DEFAULT_BACKGROUND_COLOR,
				SWT.INHERIT_DEFAULT);
	}

	public static void setBackgroundColor(Composite composite, Color color,
			int inherit) {
		Assert.isNotNull(composite);

		composite.setBackground(color);
		composite.setBackgroundMode(inherit);
	}

	public static ImageDescriptor getImageDescriptor(String path) {
		return ResourceManager.getPluginImageDescriptor(Activator.PLUGIN_ID,
				path);
	}

	public static Image getImage(String path) {
		return ResourceManager.getPluginImage(Activator.PLUGIN_ID, path);
	}

	public static IWorkbenchAdapter getAdapter(Object element) {
		IWorkbenchAdapter adapter = null;

		if (element instanceof IAdaptable) {
			adapter = (IWorkbenchAdapter) ((IAdaptable) element)
					.getAdapter(IWorkbenchAdapter.class);
		}

		if (element != null && adapter == null) {
			adapter = (IWorkbenchAdapter) Platform.getAdapterManager()
					.loadAdapter(element, IWorkbenchAdapter.class.getName());
		}

		return adapter;
	}

	public static IWorkbenchAdapter2 getAdapter2(Object element) {
		IWorkbenchAdapter2 adapter = null;

		if (element instanceof IAdaptable) {
			adapter = (IWorkbenchAdapter2) ((IAdaptable) element)
					.getAdapter(IWorkbenchAdapter2.class);
		}

		if (element != null && adapter == null) {
			adapter = (IWorkbenchAdapter2) Platform.getAdapterManager()
					.loadAdapter(element, IWorkbenchAdapter2.class.getName());
		}

		return adapter;
	}

	public static IWorkbenchAdapter3 getAdapter3(Object element) {
		IWorkbenchAdapter3 adapter = null;

		if (element instanceof IAdaptable) {
			adapter = (IWorkbenchAdapter3) ((IAdaptable) element)
					.getAdapter(IWorkbenchAdapter3.class);
		}

		if (element != null && adapter == null) {
			adapter = (IWorkbenchAdapter3) Platform.getAdapterManager()
					.loadAdapter(element, IWorkbenchAdapter3.class.getName());
		}

		return adapter;
	}

	public static IWorkbenchAdapter4 getAdapter4(Object element) {
		IWorkbenchAdapter4 adapter = null;

		if (element instanceof IAdaptable) {
			adapter = (IWorkbenchAdapter4) ((IAdaptable) element)
					.getAdapter(IWorkbenchAdapter4.class);
		}

		if (element != null && adapter == null) {
			adapter = (IWorkbenchAdapter4) Platform.getAdapterManager()
					.loadAdapter(element, IWorkbenchAdapter4.class.getName());
		}

		return adapter;
	}

	public static Color getColor(Alarm alarm) {
		if (alarm == null || alarm.getLevel() == null) {
			return null;
		}

		AlarmLevel level = alarm.getLevel();

		switch (level) {
		case CRITICAL:
			return SWTResourceManager.getColor(SWT.COLOR_DARK_RED);
		case MAJOR:
			return SWTResourceManager.getColor(SWT.COLOR_RED);
		case MINOR:
			return SWTResourceManager.getColor(SWT.COLOR_YELLOW);
		case WARNING:
			return SWTResourceManager.getColor(SWT.COLOR_CYAN);
		case INDETERMINATE:
			return SWTResourceManager.getColor(SWT.COLOR_BLUE);
		case CLEARED:
		default:
			return SWTResourceManager.getColor(SWT.COLOR_GREEN);
		}
	}

	public static void packTableColumn(Table table) {
		if (table != null) {
			TableColumn[] columns = table.getColumns();
			for (TableColumn column : columns) {
				if (column.getResizable()) {
					column.pack();
				}
			}
		}
	}
}
